package com.lc.cms.service.impl;

import com.lc.cms.dao.IRoleDao;
import com.lc.cms.dto.CreateRoleDto;
import com.lc.cms.dto.RolePermissionDto;
import com.lc.cms.model.RoleModel;
import com.lc.cms.service.IRoleService;
import com.lc.cms.utils.ResponseResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Log4j2
@Service
public class RoleServiceImpl implements IRoleService {

    private final IRoleDao roleDao;

    @Autowired
    public RoleServiceImpl(IRoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Transactional
    @Override
    public ResponseResult create(CreateRoleDto role) {
        try {
            int createDepartmentRoleResult = roleDao.createDepartmentRole(role);
            if (createDepartmentRoleResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int createResult = roleDao.create(role);
            if (createResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(RoleModel role) {
        try {
            int updateResult = roleDao.update(role);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Transactional
    @Override
    public ResponseResult delete(int roleId) {
        try {
            int deleteUserRoleResult = roleDao.deleteUserRoleByRoleId(roleId);
            if (deleteUserRoleResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int deleteDepartmentRoleResult = roleDao.deleteDepartmentRoleByRoleId(roleId);
            if (deleteDepartmentRoleResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int deleteRolePermissionResult = roleDao.deleteRolePermissionByRoleId(roleId);
            if (deleteRolePermissionResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int deleteResult = roleDao.delete(roleId);
            if (deleteResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Transactional
    @Override
    public ResponseResult updateRolePermission(RolePermissionDto role) {
        try {
            int deleteRolePermissionResult = roleDao.deleteRolePermissionByRoleId(role.getRoleId());
            if (deleteRolePermissionResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            int createRolePermissionResult = roleDao.createRolePermission(role);
            if (createRolePermissionResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
